package com.yhc.my.an_meishia.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.viewpagerindicator.TabPageIndicator;
import com.yhc.my.an_meishia.R;
import com.yhc.my.an_meishia.adapters.TabAdapter;
import com.yhc.my.an_meishia.bean.Beans;
import com.yhc.my.an_meishia.bean.HotShows;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.bmob.sms.BmobSMS;
import cn.bmob.v3.Bmob;

public class MainActivity extends FragmentActivity {
    private List<HotShows> list;
//    StringBuilder sb = new StringBuilder();
//    final String path = sb.append(Contasts.BASE_URL).append(Contasts.CATEGOTY_URL).toString();
    @ViewInject(R.id.tpi)
    private TabPageIndicator indicator;
    @ViewInject(R.id.vp)
    private ViewPager mVp;
    private DrawerLayout dr;
    private Toolbar toobar;
    private ListView mLv;
    private List<Beans> beans;

    private TabAdapter adapter;
    private ActionBarDrawerToggle mToo;
    private Map<String, String> map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BmobSMS.initialize(this, "4a05211f8d45c01ceb4ba271298a3a5b");
        Bmob.initialize(this, "4a05211f8d45c01ceb4ba271298a3a5b");




        initView();
        adapter = new TabAdapter(getSupportFragmentManager());
        mVp.setAdapter(adapter);
        indicator.setViewPager(mVp);
        //抽屉开关
        mToo = new ActionBarDrawerToggle(MainActivity.this, dr, toobar, R.string.draw_open, R.string.draw_down);
        //同步状态
        dr.addDrawerListener(mToo);
        //设置监听
        mToo.syncState();

        int[] images = images();
        final String[] names = getResources().getStringArray(R.array.names);
        final List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("text", names[i]);
            map.put("imgs", images[i]);
            list.add(map);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.drawlist_item, new String[]{"text", "imgs"}, new int[]{R.id.tv_draw, R.id.iv_draw});
        mLv.setAdapter(simpleAdapter);
        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //收藏设置
                switch (position){
                    case 1:
                       // Toast.makeText(MainActivity.this,"sc",Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(MainActivity.this,SaveActivity.class));
                        break;
                    case 5:
                        SharedPreferences video = getSharedPreferences("video", MODE_PRIVATE);
                        SharedPreferences.Editor edit = video.edit();
                        edit.clear();
                        edit.commit();
                        if (edit.commit()){
                            Toast.makeText(MainActivity.this,"清除成功",Toast.LENGTH_SHORT).show();
                        }
                }
            }
        });

    }

    private void initView() {
        dr = (DrawerLayout) findViewById(R.id.dr);
        indicator = (TabPageIndicator) findViewById(R.id.tpi);
        mVp = (ViewPager) findViewById(R.id.vp);
        toobar = (Toolbar) findViewById(R.id.toobar);
        mLv = (ListView) findViewById(R.id.lv_draw);
    }


    private int[] images() {
        return new int[]{R.mipmap.menu_comment, R.mipmap.menu_download, R.mipmap.qq, R.mipmap.qzone, R.mipmap.renren, R.mipmap.sina};
    }

    public void register(View view) {
        startActivity(new Intent(this, RegisterActivity.class));
    }

    public void login(View view) {
        startActivity(new Intent(this, LoginActivity.class));
    }
    /**
     * 判断字符串是否包含中文
     *
     * @param str
     * @return
     */
    public static boolean isContainsChinese(String str) {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(regEx);
        Matcher matcher = pat.matcher(str);
        boolean flg = false;
        if (matcher.find()) {
            flg = true;
        }
        return flg;
    }

}
